package de.uni_hamburg.informatik.mci.lineracalendar.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;

import android.view.View;

public class DayView extends View {

	// this is a test number, only for testing
	private int day = 31;
	private  float[] mdays = new float[31 * 4];
	// private Rect []rectdays = new Rect[];
	private GestureDetector det;
	private ScaleGestureDetector mScaleDetector;
	private float mStartingSpanY = 0;
	private int mcellHeight = 50;
	private int mCellHeightBeforeScaleGesture;
	private int mCellHeightmin;
	float MIN_SPANY = 10;
	private Rect rectDay = new Rect();

	private  Paint paint;

	public DayView(Context context) {
		super(context);
		mScaleDetector = new ScaleGestureDetector(context, new ScaleGesture());
		// det = new GestureDetector(context, new detector());

		init();
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		int width = MeasureSpec.getSize(widthMeasureSpec);
		int height = 31 * 100;
		setMeasuredDimension(width, height);

	}

	private void init() {
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(Color.BLUE);
		paint.setStyle(Paint.Style.STROKE);

	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.save();
//		doDrawday(canvas);
		// rect = new Rect(3, 100, canvas.getWidth(), 300);
		// doDrawDay(canvas, 1);
		// canvas.drawRect(rect, paint);
		drawTest(canvas); 

	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		rectDay.set(0, 2, getWidth(), getHeight());
		super.onSizeChanged(w, h, oldw, oldh);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// det.onTouchEvent(event);
		mScaleDetector.onTouchEvent(event);
		return true;
	}

	
	private void drawTest(Canvas canvas){
		int mDayIndex = 0;
		int k = 0;
		for (int day = 1; day <= 4; day++) {
			mdays[mDayIndex++] = rectDay.left;
			mdays[mDayIndex++] = k;// y + (k * day);
			mdays[mDayIndex++] = rectDay.right;
			mdays[mDayIndex++] = k;// y + (k * day);
			k += mcellHeight; 
	}
		canvas.drawLines(mdays, 0, mDayIndex, paint);
	
	}
	private void doDrawday(Canvas canvas) {
		int distance = 1;

		doDrawDay(canvas, distance, paint);
	}

	public void doDrawDay(Canvas canvas, int distance, Paint p) {

		int mDayIndex = 0;
		int k = 0;
		for (int day = 1; day <= 4; day++) {
			mdays[mDayIndex++] = ModuleConstants.M_MARGIN_LEFT;
			mdays[mDayIndex++] = k + distance;// y + (k * day);
			mdays[mDayIndex++] = canvas.getWidth();
			mdays[mDayIndex++] = k + distance;// y + (k * day);
			k += mcellHeight;

		}
		canvas.drawLines(mdays, 0, mDayIndex, p);

	}

	// private class detector extends GestureDetector.SimpleOnGestureListener {
	// @Override
	// public boolean onDown(MotionEvent e) {
	//
	// return true;
	// }
	//
	// @Override
	// public boolean onScroll(MotionEvent e1, MotionEvent e2,
	// float distanceX, float distanceY) {
	// Log.e("TAG", "test scroll");
	// float deltay = distanceY;
	// int initialscroll = 0;
	// initialscroll += (int) deltay;
	// // rect.set(3, 100 + initialscroll, 100, 300 - initialscroll);
	// return true;
	// }
	// }

	private class ScaleGesture extends
			ScaleGestureDetector.SimpleOnScaleGestureListener {

		@SuppressLint("NewApi")
		@Override
		public boolean onScaleBegin(ScaleGestureDetector detector) {
			mStartingSpanY = ScaleGestureDetectorCompat
					.getCurrentSpanY(detector);
			mCellHeightBeforeScaleGesture = mcellHeight;
			return true;
		}

		@Override
		public boolean onScale(ScaleGestureDetector detector) {
			Log.e(VIEW_LOG_TAG, "test on Scale");
			float spanY = ScaleGestureDetectorCompat.getCurrentSpanY(detector);
			mcellHeight = (int) (mCellHeightBeforeScaleGesture * spanY / mStartingSpanY);
			if (mcellHeight < mCellHeightmin) {
				spanY = mStartingSpanY;
				mcellHeight = mCellHeightmin;
				mCellHeightBeforeScaleGesture = mCellHeightmin;
			}

			// mRect.set(0, 2, getWidth(), getHeight() + mcellHeight);
			// mcellHeight *= detector.getScaleFactor();
			return true;
		}
		@Override
		public void onScaleEnd(ScaleGestureDetector detector) {
			mStartingSpanY = 0;
			
		}

	}
}
